package com.cl.edu.service.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.core.orm.SearchFilter;
import com.cl.core.orm.hibernate.HibernateWebUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.dao.BookingstudentDAO;
import com.cl.edu.dao.BookingteacherDAO;
import com.cl.edu.dao.MessageDAO;
import com.cl.edu.dao.TeacherDAO;
import com.cl.edu.dao.TsDAO;
import com.cl.edu.entity.Message;
import com.cl.edu.entity.Teacher;

/**
 * 教员信息管理
 * @author Administrator
 *
 */
@Service
@Transactional
public class TeachersManager {
	@Autowired
	private TeacherDAO teacherDAO;
	@Autowired
	private MessageDAO messageDAO;
	@Autowired
	private BookingstudentDAO bookingstudentDAO;
	@Autowired
	private BookingteacherDAO bookingteacherDAO;
	@Autowired
	private TsDAO tsDAO;
	
	/**
	 * 根据id得到教员信息
	 * @param id
	 * @return
	 */
	public Teacher getTeacher(Long id) {
		return teacherDAO.get(id);
	}
	
	/**
	 * 删除教员
	 * @param id
	 */
	public void delTeacher(Long id) {
		teacherDAO.delete(id);
	}
	
	/**
	 * 分页查询指定定状态的教员信息
	 * @param page
	 * @return
	 */
	public Page<Teacher> getTeachers(Page<Teacher> page,int state) {
		StringBuffer hql = new StringBuffer("from Teacher model ");
		hql.append(" where 1=1 and model.state = ").append(state);
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		for (SearchFilter filter : filters) {
			if ("id".equals(filter.getPropertyName())) {
				hql.append(" and model.id = ")
						.append(filter.getPropertyValue());
			} else if ("realname".equals(filter.getPropertyName())) {
				hql.append(" and model.realname like '%")
				.append(filter.getPropertyValue()).append("%'");
			} else if ("jianliFlag".equals(filter.getPropertyName())) {
				hql.append(" and model.jianliFlag like '%")
				.append(filter.getPropertyValue()).append("%'");
			} else if ("step".equals(filter.getPropertyName())) {
				hql.append(" and model.step like '%")
				.append(filter.getPropertyValue()).append("%'");
			}  
		}
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = teacherDAO.findPage(page, hql.toString());
		return page;
	}
	
	/**
	 * 查询满足一周之星条件的教员
	 * @param page
	 * @return
	 */
	public Page<Teacher> getWeekstarTeachers(Page<Teacher> page) {
		StringBuffer hql = new StringBuffer("from Teacher model ");
		hql.append(" where  model.state = 0 and model.photos is not null");
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = teacherDAO.findPage(page, hql.toString());
		return page;
	}
	
	/**
	 * 更新教员信息
	 * @param teacher
	 */
	public void updateTeacher(Teacher teacher){
		teacherDAO.saveOrUpdate(teacher);
	}
	
	/**
	 * 保存新的消息
	 * @param message
	 */
	public void saveMessage(Message message){
		messageDAO.save(message);
	}
	
	/**
	 * 查找已存在的每周之星数据
	 * @param star
	 * @return
	 */
	public Teacher getTeacherByStar(Integer star) {
		return teacherDAO.findUniqueBy("isWeekStar",star);
	}

	/**
	 * 删除教员主动预约学生表中指定教师编号的预约记录
	 * @param studentId
	 */
	public void delBookingstudents(Long teacherId){
		bookingstudentDAO.batchExecute("delete from Bookingstudent model where model.teacher.id="+teacherId);
	}
	
	/**
	 * 删除学生主动预约教员表中指定教师编号的预约记录
	 * @param studentId
	 */
	public void delBookingteachers(Long teacherId) {
		bookingteacherDAO.batchExecute("delete from Bookingteacher model where model.teacher.id="+teacherId);
	}

	/**
	 * 删除发送给教员的信息
	 * @param id
	 */
	public void delMessage(Long id) {
		messageDAO.batchExecute("delete from Message model where model.to="+id);
	}

	/**
	 * 删除教员的投诉信息
	 * @param id
	 */
	public void delTs(Long id) {
		tsDAO.batchExecute("delete from Ts model where model.fromId="+id);
	}
}
